/**
 * Created by DELL on 2017/4/5.
 */
window.formObject = $("form").submit(function () {
    //提交后验证表单
    var validator = $(this).validate({
        /**
         * ignore：忽略某些元素不验证
         * @type {String}
         * 例如：ignore: ".ignore" ，忽略所有的带ignore class 的标签
         */
        ignore: ".ignore",
        focusInvalid: true,
        focusCleanup:true,//以下为验证规则
        rules: {
            villageName: {
                required: true
            },
            houseAddress: {
                required: true
            },
            houseStatus: {
                required: true
            },
            houseOrientations:{
                required: true
            },
            houseFloorArea: {
                number: true,
                required: true
            },
            houseTypeOne: {
                digits: true,
                required: true
            },
            houseTypeTwo: {
                digits: true,
                required: true
            },
            houseTypeThree: {
                digits: true,
                required: true
            },
            houseFloor: {
                digits: true,
                required: true
            },
            houseFloorTotal: {
                digits: true,
                required: true
            },
            quotePrice:{
                number: true,
                required: true
            },
            decorationCondition: {
                required: true
            },
            houseType: {
                required: true
            },
            livingCondition: {
                required: true
            }
        }
    });
    //如果表单验证通过,则提交数据
    if (validator.form()) {
        var data = common.formToJson($(this));
        $.post(window.globalHost + common.collectHouseUpdatePort, data).success(function (data) {
            layer.alert(data.res.msg);
            if ("0" == data.code) {
                window.location.reload();
            }
        });
    }
    //如果表单验证不通过
    return false;
});
var houseId=common.getQueryString("houseId");
$.post(window.globalHost+common.collectHouseInfoPort,{id:houseId}).success(function (data) {
    var record=common.transformData(data);
    var list=['companyName','storeName','agentName','agentMobile'];
    list=list.map(function (ele,idx) {
        return '<span style="padding:0 5px;">' +record[ele]+'</span>/'
    }).join("");
    $("#houseAgent").html(list.substring(0,list.length-1));
    $("#recommendedReason").html(record.recommendedReason);
    common.initInputByName(record);
    common.buildNormalEnumSelect("#houseStatus",enumObject.enumJsonData['houseStatus'],record.houseStatus);
    common.buildNormalEnumSelect("#houseOrientations",enumObject.enumJsonData['houseOrientations'],record.houseOrientations);
    common.buildNormalEnumSelect("#decorationCondition",enumObject.enumJsonData['decorationCondition'],record.decorationCondition);
    common.buildNormalEnumSelect("#houseType",enumObject.enumJsonData['houseType'],record.houseType);
    common.buildNormalEnumSelect("#livingCondition",enumObject.enumJsonData['livingCondition'],record.livingCondition);
    var existImg;
    var imagePath='';
    $("#house-photos").gallery({
        readOnly:$.fn.gallery.defaults.readOnly,
        title: "照片信息",
        url: window.globalHost + common.uploadImagePort,
        paramName: "uploadData",
        fileFolder: {folder: "house",id:record.agentId},
        returnPathName: "filePath",
        filePaths: record.housePhotos ? record.housePhotos.split(',') : [],
        uploadCallback: function (filePath) {
            existImg = $("input[name='housePhotos']").val();
            if (existImg) {
                var existImgArr = existImg.split(',');
                for (var i = 0; i < existImgArr.length; i++) {
                    if (existImgArr[i] === filePath) {
                        existImgArr.splice(i, 1);
                    }
                }
                imagePath = existImgArr.join(',') + ',' + filePath + ',';
            } else {
                imagePath += filePath + ',';
            }

            $('input[type=hidden]').filter('[name="housePhotos"]').val(imagePath.substring(0, imagePath.length - 1));
        },
        deleteCallback: function (filePath) {
            existImg = $("input[name='housePhotos']").val();
            if (existImg) {
                var imgArr = existImg.split(',');
                for (var i = 0; i < imgArr.length; i++) {
                    if (imgArr[i] === filePath) {
                        imgArr.splice(i, 1);
                    }
                }
            }
            $('input[type=hidden]').filter('[name="housePhotos"]').val(imgArr.join(','));
        }
    });
});

